package org.jeecg.modules.basic.exitstock.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.exitstock.entity.LwgyMaterialExitstock;
import org.jeecg.modules.basic.exitstock.service.ILwgyMaterialExitstockService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.basic.goods.entity.LwgyMaterialGoods;
import org.jeecg.modules.basic.material.entity.LwgyInStock;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplication;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplications;
import org.jeecg.modules.basic.plan.vo.LwgyScoreSanitationPage;
import org.jeecg.modules.basic.storages.entity.LwgyStoragesNum;
import org.jeecg.modules.basic.storages.service.ILwgyStoragesNumService;
import org.jeecg.modules.basic.worker.entity.LwgyMaterialWorker;
import org.jeecg.modules.basic.worker.service.ILwgyMaterialWorkerService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 物料管理退库表
 * @Author: jeecg-boot
 * @Date:   2021-04-06
 * @Version: V1.0
 */
@Api(tags="物料管理退库表")
@RestController
@RequestMapping("/exitstock/lwgyMaterialExitstock")
@Slf4j
public class LwgyMaterialExitstockController extends JeecgController<LwgyMaterialExitstock, ILwgyMaterialExitstockService> {
	@Autowired
	private ILwgyMaterialExitstockService lwgyMaterialExitstockService;

	 @Autowired
	 private ILwgyStoragesNumService lwgyStoragesNumService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;

	 @Autowired
	 private ILwgyMaterialWorkerService lwgyMaterialWorkerService;
	
	/**
	 * 分页列表查询
	 *
	 * @param lwgyMaterialExitstock
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "物料管理退库表-分页列表查询")
	@ApiOperation(value="物料管理退库表-分页列表查询", notes="物料管理退库表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyMaterialExitstock lwgyMaterialExitstock,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyMaterialExitstock> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialExitstock, req.getParameterMap());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		if (!roles.contains("admin")) {
			queryWrapper.eq(" exit_stock_user",sysUser.getUsername());
		}
		Page<LwgyMaterialExitstock> page = new Page<LwgyMaterialExitstock>(pageNo, pageSize);
		IPage<LwgyMaterialExitstock> pageList = lwgyMaterialExitstockService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @AutoLog(value = "-自定义分页列表查询")
	 @ApiOperation(value = "-自定义分页列表查询", notes = "-自定义分页列表查询")
	 @GetMapping(value = "/list2")
	 public Result<?> queryCustomersList(LwgyMaterialExitstock lwgyMaterialExitstock,
										 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
										 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
										 @RequestParam(name="id",required=true) String id,
										 HttpServletRequest req) {
		 QueryWrapper<LwgyMaterialExitstock> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialExitstock, req.getParameterMap());
		 Page<LwgyMaterialExitstock> page = new Page<>(pageNo, pageSize);
		 IPage<LwgyMaterialExitstock> pageList = lwgyMaterialExitstockService.queryPageCustomer(page, queryWrapper,id);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param lwgyMaterialExitstock
	 * @return
	 */
	@AutoLog(value = "物料管理退库表-添加")
	@ApiOperation(value="物料管理退库表-添加", notes="物料管理退库表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyMaterialExitstock lwgyMaterialExitstock) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		lwgyMaterialExitstock.setExitStockUser(sysUser.getUsername());
		lwgyMaterialExitstock.setExitStockTime(new Date());
		lwgyMaterialExitstock.setExitState("1");//直接审核通过
		lwgyMaterialExitstockService.save(lwgyMaterialExitstock);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param lwgyMaterialExitstock
	 * @return
	 */
	@AutoLog(value = "物料管理退库表-编辑")
	@ApiOperation(value="物料管理退库表-编辑", notes="物料管理退库表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyMaterialExitstock lwgyMaterialExitstock) {
		lwgyMaterialExitstockService.updateById(lwgyMaterialExitstock);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "物料管理退库表-通过id删除")
	@ApiOperation(value="物料管理退库表-通过id删除", notes="物料管理退库表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyMaterialExitstockService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "物料管理退库表-批量删除")
	@ApiOperation(value="物料管理退库表-批量删除", notes="物料管理退库表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyMaterialExitstockService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "物料管理退库表-通过id查询")
	@ApiOperation(value="物料管理退库表-通过id查询", notes="物料管理退库表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyMaterialExitstock lwgyMaterialExitstock = lwgyMaterialExitstockService.getById(id);
		if(lwgyMaterialExitstock==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyMaterialExitstock);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyMaterialExitstock
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyMaterialExitstock lwgyMaterialExitstock) {
        return super.exportXls(request, lwgyMaterialExitstock, LwgyMaterialExitstock.class, "物料管理退库表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyMaterialExitstock.class);
    }

	 /**
	  *   添加
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "物料退库表-退库")
	 @ApiOperation(value="物料退库表-退库", notes="物料退库表-退库")
	 @PutMapping(value = "/adds")
	 public Result<?> adds(@RequestBody LwgyMaterialExitstock lwgyMaterialExitstock) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 lwgyMaterialExitstock.setReviewer(sysUser.getUsername());
		 lwgyMaterialExitstock.setReviewTime(new Date());
		 lwgyMaterialExitstock.setExitState("3");
		 lwgyMaterialExitstockService.updateById(lwgyMaterialExitstock);
		 LwgyStoragesNum lwgyStoragesNum=new LwgyStoragesNum();
		 List<LwgyStoragesNum> list=lwgyStoragesNumService.queryPageList(lwgyMaterialExitstock.getGoodsId(),lwgyMaterialExitstock.getName());//可能会有问题 2023.4.13
		 if(list.size()>=1){
			 lwgyStoragesNum = list.get(0);
			 BigDecimal a = lwgyStoragesNum.getNum();
			 System.out.println("1111:"+a);
			 BigDecimal b = lwgyMaterialExitstock.getAmount();
			 System.out.println("2222:"+b);
			 BigDecimal c=a.add(b);
			 System.out.println("cc:"+c);
			 lwgyStoragesNum.setNum(c);
			 lwgyStoragesNumService.updateById(lwgyStoragesNum);
		 }else{
			 lwgyStoragesNum.setCkId(lwgyMaterialExitstock.getName());
			 lwgyStoragesNum.setGoodsId(lwgyMaterialExitstock.getGoodsId());
			 lwgyStoragesNum.setMinCount(new BigDecimal("0.00"));
			 BigDecimal a = lwgyMaterialExitstock.getAmount();
			 lwgyStoragesNum.setNum(a);
			 lwgyStoragesNum.setPrice(BigDecimal.ZERO);
			 lwgyStoragesNumService.save(lwgyStoragesNum);
		 }
		 // 将维修人员现有的物料数量-退库的数量
		 LwgyMaterialWorker lwgyMaterialWorker=new LwgyMaterialWorker();
		 List< LwgyMaterialWorker> list2=lwgyMaterialWorkerService.queryPageLists(lwgyMaterialExitstock.getGoodsId());
		 System.out.println("list222:"+list2);
		 if(list2.size()>=1) {
		 	lwgyMaterialWorker=list2.get(0);
			 BigDecimal b=lwgyMaterialWorker.getApplyNum();
			 System.out.println("bbbb:"+b);
			 BigDecimal c=lwgyMaterialExitstock.getAmount();
			 System.out.println("cccc:"+c);
			 BigDecimal d=b.subtract(c);
			 System.out.println("dddddd:"+d);
			 lwgyMaterialWorker.setApplyNum(d);
			 lwgyMaterialWorkerService.updateById(lwgyMaterialWorker);
		 }
		 return Result.OK("添加成功！");
	 }
}
